using PK.Broadcaster;
//using PK.InAppPurchase;
using PKGames;
using PKGames.Audio;
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

namespace VoxelGame
{
	public class WorldSelectionController : MonoBehaviour
        //, IMessageSubscriber<ProcessPurchaseSuccessMessage>
	{
		private static bool isExplorerPainter;

		[SerializeField]
		private CanvasScaler _canvasScaler;

		[SerializeField]
		private RectTransform _cellAnchor;

		[SerializeField]
		private GridLayoutGroup _cellGrid;

		[SerializeField]
		private RectTransform _viewRect;

		[SerializeField]
		private RectTransform _content;

		[SerializeField]
		private GameObject _cellPrefab;

		[SerializeField]
		private WorldSelectionList _worldsList;

		[SerializeField]
		private static WorldSelectionList _explorerWorldsList;

		[SerializeField]
		private BottomFloorSizeController _bottomFloorSizeController;

		private WorldSelectionCellData[] _worldSelectionCellData;

		private VoxelWorldSelectorGridLayouter _gridLayouter;

		private GridLayoutDataSource _dataSource;

		private const string _gameName = "voxel";

		public static bool IsExplorerPainter
		{
			get
			{
				return isExplorerPainter;
			}
			set
			{
				UnityEngine.Debug.Log("<color=red>[WorldSelectionController]</color> Switching games to explorer mode = " + value);
				isExplorerPainter = value;
			}
		}

		public static WorldSelectionList ExplorerWorldsList
		{
			get
			{
				return _explorerWorldsList;
			}
			set
			{
				_explorerWorldsList = value;
			}
		}

		public WorldSelectionList WorldsList
		{
			get
			{
				if (IsExplorerPainter)
				{
					return ExplorerWorldsList;
				}
				return _worldsList;
			}
			set
			{
				if (IsExplorerPainter)
				{
					ExplorerWorldsList = value;
				}
				_worldsList = value;
			}
		}

		private void UnpackCellData()
		{
			_worldSelectionCellData = new WorldSelectionCellData[WorldsList.Length()];
			for (int i = 0; i < WorldsList.Length(); i++)
			{
				_worldSelectionCellData[i] = new WorldSelectionCellData(WorldsList.Configs[i]);
			}
			_gridLayouter = new VoxelWorldSelectorGridLayouter(_cellAnchor, _cellGrid, _content, _worldSelectionCellData.Length, _viewRect, _canvasScaler);
			_gridLayouter.UpdateLayout();
		}

		private IEnumerator Start()
		{
			MonoSingleton<AudioManager>.Instance.PlayBackgroundMusic();
			//Application.targetFrameRate = 60;
			yield return new WaitForEndOfFrame();
			UnpackCellData();
			PopulateCellGrid();
			_bottomFloorSizeController.PositionBottomTile(_canvasScaler, _viewRect, _cellAnchor, _content);
		}

		private void PopulateCellGrid()
		{
			for (int i = 0; i < _worldSelectionCellData.Length; i++)
			{
				GameObject gameObject = UnityEngine.Object.Instantiate(_cellPrefab);
				IGridCell component = gameObject.GetComponent<IGridCell>();
				component.UpdateCell(_worldSelectionCellData[i]);
				gameObject.transform.SetParent(_cellGrid.transform, worldPositionStays: false);
			}
		}

		private void PopulateRecyclabeCellGrid()
		{
			_dataSource = new GridLayoutDataSource(_cellPrefab, _worldSelectionCellData, _cellAnchor, _cellGrid, _content, _viewRect);
			_dataSource.PopulateCellGrid();
			StartCoroutine(_dataSource.RecycleList());
		}

		private void Awake()
		{
			//MessageBroadcaster.Instance.Subscribe(this);
		}

		private void OnDestroy()
		{
			//MessageBroadcaster.Instance.Unsubscribe(this);
			Resources.UnloadUnusedAssets();
			GC.Collect();
		}

		//public void OnMessageReceived(ProcessPurchaseSuccessMessage message)
		//{
		//	WorldSelectionCell[] componentsInChildren = _cellGrid.transform.GetComponentsInChildren<WorldSelectionCell>();
		//	WorldSelectionCell[] array = componentsInChildren;
		//	foreach (WorldSelectionCell worldSelectionCell in array)
		//	{
		//		StoreProduct product = ((WorldSelectionCellData)worldSelectionCell.CellData).WorldSelectionConfig.Product;
		//		if (product != null && product.StoreId == message.StoreProduct.StoreId)
		//		{
		//			StartCoroutine(worldSelectionCell.UpdateCell());
		//		}
		//	}
		//	MonoSingleton<IapCreativeManager>.Instance.CloseIapCreative();
		//}
	}
}
